package com.zx.notification.common.config;

import com.zx.notification.common.service.ConfigChangeListener;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * Redis配置类
 * 配置Redis消息监听器容器和订阅主题
 */
@Configuration
@RequiredArgsConstructor
public class RedisConfig {

    private final ConfigChangeListener configChangeListener;

    /**
     * 配置Redis消息监听器容器
     *
     * @param connectionFactory Redis连接工厂
     * @return Redis消息监听器容器
     */
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(configChangeListener, channelTopic());
        return container;
    }

    /**
     * 配置订阅主题
     *
     * @return 订阅主题
     */
    @Bean
    public ChannelTopic channelTopic() {
        return new ChannelTopic("notification:config:reload");
    }
}